<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="订单描述" prop="description">-->
<!--        <el-input-->
<!--          v-model="queryParams.description"-->
<!--          placeholder="请输入订单描述"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="套餐" prop="orderPackage">
<!--        <el-input-->
<!--          v-model="queryParams.orderPackage"-->
<!--          placeholder="请输入套餐"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
        <el-select
          v-model="queryParams.orderPackage"
          placeholder="套餐"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in order_package"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="金额" prop="amount">-->
<!--        <el-input-->
<!--          v-model="queryParams.amount"-->
<!--          placeholder="请输入金额"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="货币种类" prop="currency">
<!--        <el-input-->
<!--          v-model="queryParams.currency"-->
<!--          placeholder="请输入货币种类"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
        <el-select
          v-model="queryParams.currency"
          placeholder="货币种类"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in currency_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="支付订单id" prop="payId">-->
<!--        <el-input-->
<!--          v-model="queryParams.payId"-->
<!--          placeholder="请输入支付订单id"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="交易流水号" prop="transNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.transNo"-->
<!--          placeholder="请输入交易流水号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="支付者账号" prop="payerEmail">-->
<!--        <el-input-->
<!--          v-model="queryParams.payerEmail"-->
<!--          placeholder="请输入支付者账号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="支付时间" prop="payTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.payTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择支付时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['userOrder:userOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['userOrder:userOrder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['userOrder:userOrder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['userOrder:userOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userOrderList" @selection-change="handleSelectionChange" style="line-height: 40px;">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键Id" align="center" prop="id" v-if="true"/>-->
      <el-table-column label="姓名" align="center" prop="username" />
      <el-table-column label="电话" align="center" prop="mobile" min-width="140px"/>
      <el-table-column label="地址" align="center" prop="address" width="400px" :show-overflow-tooltip="true"/>
      <el-table-column label="订单号" align="center" prop="orderId" min-width="220px"/>
<!--      <el-table-column label="订单描述" align="center" prop="description" />-->
      <el-table-column label="套餐" align="center" prop="orderPackage" >
<!--        <template #default="scope">-->
<!--          <dict-tag :options="order_package" :value="scope.row.orderPackage"/>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="金额" align="center" prop="amount">

      </el-table-column>
      <el-table-column label="货币种类" align="center" prop="currency">
        <template #default="scope">
          <dict-tag :options="currency_type" :value="scope.row.currency"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="支付订单id" align="center" prop="payId" />-->
<!--      <el-table-column label="交易流水号" align="center" prop="transNo" />-->
      <el-table-column label="支付方式" align="center" prop="payType" >
        <template #default="scope">
          <dict-tag :options="pay_type" :value="scope.row.payType"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="支付者账号" align="center" prop="payerEmail" />-->
<!--      <el-table-column label="支付时间" align="center" prop="payTime" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="订单状态" align="center" prop="orderStatus" >
        <template #default="scope">
          <dict-tag :options="order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime" width="180">
              <template #default="scope">
                <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
              </template>
            </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"  :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" min-width="180px">
          <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['userOrder:userOrder:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['userOrder:userOrder:remove']">删除</el-button>
          </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户订单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="userOrderRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单号" disabled/>
        </el-form-item>
        <el-form-item label="订单描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入订单描述" />
        </el-form-item>
        <el-form-item label="套餐" prop="orderPackage">
<!--          <el-input v-model="form.orderPackage" placeholder="请输入套餐" />-->
          <el-select v-model="form.orderPackage" placeholder="请选择" @change="changeAmount">
            <el-option
              v-for="dict in order_package"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"

            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入金额" disabled/>
        </el-form-item>
        <el-form-item label="货币种类" prop="currency">
<!--          <el-input v-model="form.currency" placeholder="请输入货币种类" />-->
          <el-select v-model="form.currency" placeholder="请选择">
            <el-option
              v-for="dict in currency_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付方式" prop="payType">
          <el-select v-model="form.payType" placeholder="请选择">
            <el-option
              v-for="dict in pay_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单状态" prop="orderStatus">
          <el-select v-model="form.orderStatus" placeholder="请选择">
            <el-option
              v-for="dict in order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="支付订单id" prop="payId">-->
<!--          <el-input v-model="form.payId" placeholder="请输入支付订单id" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="交易流水号" prop="transNo">-->
<!--          <el-input v-model="form.transNo" placeholder="请输入交易流水号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="支付者账号" prop="payerEmail">-->
<!--          <el-input v-model="form.payerEmail" placeholder="请输入支付者账号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="支付时间" prop="payTime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.payTime"-->
<!--            type="datetime"-->
<!--            value-format="YYYY-MM-DD HH:mm:ss"-->
<!--            placeholder="请选择支付时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="UserOrder">
import { listUserOrder, getUserOrder, delUserOrder, addUserOrder, updateUserOrder } from "@/api/userOrder/userOrder";

const { proxy } = getCurrentInstance();
const {currency_type,order_status,pay_type,order_package} = proxy.useDict("currency_type","order_status","pay_type","order_package")
const userOrderList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    username: undefined,
    mobile: undefined,
    address: undefined,
    orderId: undefined,
    description: undefined,
    orderPackage: undefined,
    amount: undefined,
    currency: undefined,
    payId: undefined,
    transNo: undefined,
    payType: undefined,
    payerEmail: undefined,
    payTime: undefined,
    orderStatus: undefined,
  },
  rules: {
    // id: [
    //   { required: true, message: "主键Id不能为空", trigger: "blur" }
    // ],
    username: [
      { required: true, message: "姓名不能为空", trigger: "blur" }
    ],
    mobile: [
      { required: true, message: "电话不能为空", trigger: "blur" }
    ],
    address: [
      { required: true, message: "地址不能为空", trigger: "blur" }
    ],
    // orderId: [
    //   { required: true, message: "订单号不能为空", trigger: "blur" }
    // ],
    // description: [
    //   { required: true, message: "订单描述不能为空", trigger: "blur" }
    // ],
    orderPackage: [
      { required: true, message: "套餐不能为空", trigger: "blur" }
    ],
    // amount: [
    //   { required: true, message: "金额不能为空", trigger: "blur" }
    // ],
    currency: [
      { required: true, message: "货币种类不能为空", trigger: "blur" }
    ],
    // payId: [
    //   { required: true, message: "支付订单id不能为空", trigger: "blur" }
    // ],
    // transNo: [
    //   { required: true, message: "交易流水号不能为空", trigger: "blur" }
    // ],
    payType: [
      { required: true, message: "支付方式，0在线支付,1货到付款不能为空", trigger: "change" }
    ],
    // payerEmail: [
    //   { required: true, message: "支付者账号不能为空", trigger: "blur" }
    // ],
    // payTime: [
    //   { required: true, message: "支付时间不能为空", trigger: "blur" }
    // ],
    // orderStatus: [
    //   { required: true, message: "订单状态，0未支付，1支付 2 取消 3超时不能为空", trigger: "change" }
    // ],
    // remark: [
    //   { required: true, message: "备注不能为空", trigger: "blur" }
    // ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户订单列表 */
function getList() {
  loading.value = true;
  listUserOrder(queryParams.value).then(response => {
    userOrderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    username: null,
    mobile: null,
    address: null,
    orderId: null,
    description: null,
    orderPackage: null,
    amount: null,
    currency: null,
    payId: null,
    transNo: null,
    payType: null,
    payerEmail: null,
    payTime: null,
    orderStatus: null,
    remark: null,
    delFlag: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null
  };
  proxy.resetForm("userOrderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加用户订单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _id = row.id || ids.value
  getUserOrder(_id).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改用户订单";
  });
}

function changeAmount(event) {
  // console.log(event);
  for (let item in order_package.value) {
    let obj = order_package.value[item];
    // console.log("obj:"+item);
    // console.log(item.label);
    if (obj.label == event) {
      console.log(item.value);
      data.form.amount=obj.value;
    }
  }
  // console.log(event.target.label);
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["userOrderRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateUserOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addUserOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除用户订单编号为"' + _ids + '"的数据项？').then(function() {
    loading.value = true;
    return delUserOrder(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('userOrder/userOrder/export', {
    ...queryParams.value
  }, `userOrder_${new Date().getTime()}.xlsx`)
}

getList();
</script>
